
package com.palm.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;


/**
 * Spring初始化实现
 *
 * @author Kama Luther
 * @version 0.1
 * @since 0.1
 */
public class SpringInitListener extends ContextLoaderListener {

	private static final Logger logger = LoggerFactory.getLogger(SpringInitListener.class);

	// --------------------------------------------------------------------------

	private static ApplicationContext springContext;

	public static ApplicationContext getApplicationContext() {
		return springContext;
	}

	public static Object getBean(String name) {
		return springContext.getBean(name);
	}

	// --------------------------------------------------------------------------

	@Override
	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);

		ServletContext sc = event.getServletContext();
		springContext = WebApplicationContextUtils.getWebApplicationContext(sc);
		logger.info("SpringInit Initiallized.");
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		

		
	}

	public static void setApplicationContext(ApplicationContext ct) {
		springContext = ct;
	}

	// --------------------------------------------------------------------------

}
